# -*- coding: utf-8 -*-

#  Copyright (C) 2008 - Eugene Khorev
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330,
#  Boston, MA 02111-1307, USA.

import pygtk
pygtk.require("2.0")
import gtk

class bookmark_list(object):
    
    def __init__(self, config):
        self._list = {}
        self._config = config
        
    def get_store(self, uri): # Gets tree store for an uri
        try:
            return self._list[uri]["store"]
        except:
            return None
        
    def add(self, uri, line, comment): # Adds a line for an uri (returns True on success)
        exists = self.exists(uri, line)
        
        if not exists:
            if self._list.has_key(uri):
                self._list[uri]["iters"][line] = self._list[uri]["store"].append([line, comment])
            else:
                store = gtk.ListStore(int, str)
                self._list[uri] = {"store": store, "iters": {line: store.append([line, comment])}}
        
        return not exists
        
    def delete(self, uri, line = None): # Deletes a line or an entire uri (returns True on success)
        if line:
            exists = self.exists(uri, line)
            
            if exists:
                self._list[uri]["store"].remove(self._list[uri]["iters"][line])
                del self._list[uri]["iters"][line]
                
            return exists
        else:
            try:
                del self._list[uri]
                return True
            except:
                return False
        
    def exists(self, uri, line): # Returns True if there is a line exists in an uri
        try:
            return self._list[uri]["iters"][line]
        except:
            return False
        
    def toggle(self, uri, line, comment = ""): # Adds or removes a line for an uri
        if self.exists(uri, line):
            self.delete(uri, line)
        else:
            self.add(uri, line, comment)

# ex:ts=4:et:
